package com.neusoft.student.controller;

import com.neusoft.student.model.Student;
import com.neusoft.student.service.StudentService;
import com.neusoft.student.service.impl.StudentServiceImpl;
import com.neusoft.student.utils.Pager;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author: duan
 * @Date: Create in 16:21 2020/10/21
 */
@WebServlet("/studentlist")
public class StudentListServlet extends HttpServlet {
    private StudentService studentService = new StudentServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("StudentListServlet:::doPost");
        //获取参数
        String search = req.getParameter("search");
        String pageNumStr = req.getParameter("pageNum");
//        List<Student> list = studentService.queryList(search);
        int pageSize = 5;
        int pageNum = Integer.valueOf(pageNumStr == null ? "1" : pageNumStr);
        List<Student> list = studentService.queryListByPage((pageNum - 1) * pageSize, pageSize);
        req.setAttribute("list", list);
        Pager pager = new Pager(pageSize);
        pager.setCount(studentService.getCount());
        pager.setPageNum(pageNum);
        req.setAttribute("pager", pager);
//        req.setAttribute("count", studentService.getCount());
//        req.setAttribute("pageSize", pageSize);
//        req.setAttribute("pageNum", Integer.valueOf(pageNum));
        req.getRequestDispatcher("/student_list.jsp").forward(req, resp);
//        resp.sendRedirect("/student_list.jsp");
    }
}
